package com.example.enterprise

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.enterprise.adapter.EnterpriseAdapter
import com.example.enterprise.databinding.ActivityEnterpriseBinding
import com.example.enterprise.vm.EnterpriseViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route (path = "/enterprise/EnterpriseActivity")
class EnterpriseActivity : BaseMvvmActivity<ActivityEnterpriseBinding>() {
    override fun getLayoutId(): Int =R.layout.activity_enterprise
    val enterpriseAdapter= EnterpriseAdapter()
    private val enterpriseViewModel:EnterpriseViewModel by viewModels()
    var name:String=""
    override fun initView() {
        super.initView()
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }
        mDataBinding.gotoMerchant.setOnClickListener {
            ARouter.getInstance().build("/enterprise/MerchantActivity")
                .navigation()
        }
        mDataBinding.sv.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (query!=null){
                    name=query
                    enterpriseViewModel.getEnterprise()
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText!=null){
                    name=""
                    enterpriseViewModel.getEnterprise()
                }
                return false
            }

        })
        mDataBinding.rv.layoutManager= LinearLayoutManager(this)
        mDataBinding.rv.adapter=enterpriseAdapter
        enterpriseViewModel.mSuccessLiveData.observe(this){
            enterpriseAdapter.data.clear()
            if (name.isNullOrEmpty()){
                enterpriseAdapter.data.addAll(it.data)
            }else{
                enterpriseAdapter.data.addAll(it.data.filter { it.name.contains(name) })
            }
            enterpriseAdapter.notifyDataSetChanged()
        }
        enterpriseViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
        //点击去详情
        enterpriseAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/enterprise/EnterpriseXiangActivity")
                .withInt("enterpriseId",enterpriseAdapter.data[position].id)
                .navigation()

        }
    }

    override fun onResume() {
        super.onResume()
        enterpriseViewModel.getEnterprise()
    }
}